2 # Class to represent an image
3 # Provides methods to retrieve paths (physical, logical, URL),
4 # to generate thumbnails or for uploading.
9 var $name, # name of the image
10 $imagePath, # Path of the image
12 $title, # Title object for this image. Initialized when needed.
13 $fileExists, # does the image file exist on disk?
14 $historyLine, # Number of line to return by nextHistoryLine()
15 $historyRes, # result of the query for the image's history
17 $height, # --- returned by getimagesize, see http://de3.php.net/manual/en/function.getimagesize.php
23 function Image( $name )
25 global $wgUploadDirectory;
28 $this->title
= Title
::makeTitle( Namespace::getImage(), $this->name
);
29 //$this->imagePath = wfImagePath( $name );
30 $hash = md5( $this->title
->getDBkey() );
31 $this->imagePath
= $wgUploadDirectory . "/" . $hash{0} . "/" .substr( $hash, 0, 2 ) . "/{$name}";
33 $this->url
= $this->wfImageUrl( $name );
35 if ( $this->fileExists
= file_exists( $this->imagePath
) ) // Sic!, "=" is intended
37 list($this->width
, $this->height
, $this->type
, $this->attr
) = getimagesize( $this->imagePath
);
39 $this->historyLine
= 0;
42 function newFromTitle( $nt )
44 $img = new Image( $nt->getDBKey() );
59 function getImagePath()
61 return $this->imagePath
;
79 function getEscapeLocalURL()
81 return $this->title
->escapeLocalURL();
84 function wfImageUrl( $name )
89 $url = "{$wgUploadPath}/" . $hash{0} . "/" .
90 substr( $hash, 0, 2 ) . "/{$name}";
91 return wfUrlencode( $url );
97 return $this->fileExists
;
100 function thumbUrl( $width, $subdir="thumb" ) {
101 global $wgUploadPath;
103 $name = $this->thumbName( $width );
104 $hash = md5( $name );
105 $url = "{$wgUploadPath}/{$subdir}/" . $hash{0} . "/" . substr( $hash, 0, 2 ) . "/{$name}";
107 return wfUrlencode($url);
110 function thumbName( $width ) {
111 return $width."px-".$this->name
;
114 //**********************************************************************
115 // Create a thumbnail of the image having the specified width.
116 // The thumbnail will not be created if the width is larger than the
117 // image's width. Let the browser do the scaling in this case.
118 // The thumbnail is stored on disk and is only computed if the thumbnail
119 // file does not exist OR if it is older than the image.
120 function createThumb( $width ) {
121 global $wgUploadDirectory;
122 global $wgImageMagickConvertCommand;
123 global $wgUseImageMagick;
124 global $wgUseSquid, $wgInternalServer;
125 $thumbName = $this->thumbName( $width );
126 $thumbPath = wfImageThumbDir( $thumbName )."/".$thumbName;
127 $thumbUrl = $this->thumbUrl( $width );
129 if ( ! $this->exists() )
131 # If there is no image, there will be no thumbnail
135 # Sanity check $width
136 $width = IntVal( $width );
141 if( $width > $this->width
) {
142 # Don't make an image bigger than the source
143 return $this->getURL();
146 if ( (! file_exists( $thumbPath ) )
147 ||
( filemtime($thumbPath) < filemtime($this->imagePath
) ) ) {
151 $wgInternalServer.$thumbUrl
153 wfPurgeSquidServers($urlArr);
156 if ( $wgUseImageMagick ) {
158 $cmd = $wgImageMagickConvertCommand .
159 " -quality 85 -geometry {$width} ".
160 escapeshellarg($this->imagePath
) . " " .
161 escapeshellarg($thumbPath);
162 $conv = shell_exec( $cmd );
164 # Use PHP's builtin GD library functions.
166 # First find out what kind of file this is, and select the correct
167 # input routine for this.
169 switch( $this->type
) {
171 $src_image = imagecreatefromgif( $this->imagePath
);
174 $src_image = imagecreatefromjpeg( $this->imagePath
);
177 $src_image = imagecreatefrompng( $this->imagePath
);
179 case 15: # WBMP for WML
180 $src_image = imagecreatefromwbmp( $this->imagePath
);
183 $src_image = imagecreatefromxbm( $this->imagePath
);
186 return "Image type not supported";
189 $height = floor( $this->height
* ( $width/$this->width
) );
190 $dst_image = imagecreatetruecolor( $width, $height );
191 imagecopyresampled( $dst_image, $src_image,
193 $width, $height, $this->width
, $this->height
);
194 switch( $this->type
) {
199 #$thumbUrl .= ".png";
200 #$thumbPath .= ".png";
201 imagepng( $dst_image, $thumbPath );
204 #$thumbUrl .= ".jpg";
205 #$thumbPath .= ".jpg";
206 imageinterlace( $dst_image );
207 imagejpeg( $dst_image, $thumbPath, 95 );
212 imagedestroy( $dst_image );
213 imagedestroy( $src_image );
218 # Check for zero-sized thumbnails. Those can be generated when
219 # no disk space is available or some other error occurs
221 $thumbstat = stat( $thumbPath );
222 if( $thumbstat["size"] == 0 )
224 unlink( $thumbPath );
229 } // END OF function createThumb
231 //**********************************************************************
232 // Return the image history of this image, line by line.
233 // start with current version, than old versions.
234 // use $this->historyLine to check which line to return:
235 // 0 return line for current version
236 // 1 query for old versions, return first one
237 // 2, ... return next old version from above query
238 function nextHistoryLine()
240 $fname = "Image::nextHistoryLine()";
242 if ( $this->historyLine
== 0 ) // called for the first time, return line from cur
244 $sql = "SELECT img_size,img_description,img_user," .
245 "img_user_text,img_timestamp, '' AS oi_archive_name FROM image WHERE " .
246 "img_name='" . wfStrencode( $this->title
->getDBkey() ) . "'";
247 $this->historyRes
= wfQuery( $sql, DB_READ
, $fname );
249 if ( 0 == wfNumRows( $this->historyRes
) ) { return FALSE; }
251 } else if ( $this->historyLine
== 1 )
253 $sql = "SELECT oi_size AS img_size, oi_description AS img_description," .
254 "oi_user AS img_user," .
255 "oi_user_text AS img_user_text, oi_timestamp AS img_timestamp , oi_archive_name FROM oldimage WHERE " .
256 "oi_name='" . wfStrencode( $this->title
->getDBkey() ) . "' " .
257 "ORDER BY oi_timestamp DESC";
258 $this->historyRes
= wfQuery( $sql, DB_READ
, $fname );
260 $this->historyLine ++
;
262 return wfFetchObject( $this->historyRes
);
265 function resetHistory()
267 $this->historyLine
= 0;